<?php
namespace app\service;

/*
 * 滑块验证
 */
class Slide
{
    private $im = null;
    private $im_fullbg = null;
    private $im_bg = null;
    private $im_slide = null;
    private $bg_width = 240;
    private $bg_height = 150;
    private $mark_width = 50;
    private $mark_height = 50;
    private $bg_num = 6;
    private $x = 0;
    private $y = 0;
    //容错象素 越大体验越好，越小破解难道越高
    private $fault = 3;

    function _construct(array $config)
    {
        isset($config['bg_width']) && ($this->bg_width = $config['bg_width']);
        isset($config['bg_height']) && ($this->bg_height = $config['bg_height']);
        isset($config['mark_width']) && ($this->mark_width = $config['mark_width']);
        isset($config['mark_height']) && ($this->mark_height = $config['mark_height']);
        isset($config['bg_num']) && ($this->bg_num = $config['bg_num']);
        isset($config['fault']) && ($this->fault = $config['fault']);
    }
    /**
     * Undocumented function
     *
     * @return void
     */
    public function make()
    {
        $this->init();
        $this->create(); //创建滑动图
        $this->bg(); //创建背景图
        $this->merge(); //合并图
        $this->imgout(); //输出图
        // $imgName = $this->imgout();
        $this->destroy(); //注销
        // return $imgName;
    }
    
    public function check(int $offset = 0){
        $ret = abs(session('slide-x') - $offset) <= $this->fault;
        if($ret){
            session('slide-x', null);
        }else{
            $num = session('slide-err');
            session('slide-err', $num + 1);
            if(session('slide-err') > 5){//错误5次必须刷新
                session('slide-x', null);
            }
        }
        return $ret;
    }
    
    private function init()
    {
        $bg = mt_rand(1, $this->bg_num);  // 1-6随机数
        $file_bg = public_path() . 'upload/bg/' . $bg . '.png';
        $this->im_fullbg = imagecreatefrompng($file_bg);  //内置php 函数编辑图片
        //用于创建新的true-color图像。此函数返回给定尺寸的空白图像。
        $this->im_bg = imagecreatetruecolor($this->bg_width, $this->bg_height);
        imagecolortransparent($this->im_bg, null);
        //用于复制图像或图像的一部分
        imagecopy($this->im_bg, $this->im_fullbg,0,0,0,0,$this->bg_width, $this->bg_height);
    
        $this->im_slide = imagecreatetruecolor($this->mark_width, $this->bg_height);
        $this->x = mt_rand(50, $this->bg_width - $this->mark_width-1);
        session('slide-x', $this->x);
        session('slide-err', 0);
        $this->y = mt_rand(0, $this->bg_height-$this->mark_height-1);
    }
    
    private function destroy()
    {
        imagedestroy($this->im);
        imagedestroy($this->im_fullbg);
        imagedestroy($this->im_bg);
        imagedestroy($this->im_slide);
    }
    private function imgout()
    {
        // if(!$_GET['nowebp']&&function_exists('imagewebp')){//优先webp格式，超高压缩率
        //     $type = 'webp';
        //     $quality = 40;//图片质量 0-100
        // }else{
        //     $type = 'png';
        //     $quality = 7;//图片质量 0-9
        // }
        $type = 'png';
        $quality = 7;//图片质量 0-9
        header('Content-Type: image/'.$type);
        $func = 'image' . $type; //imagepng
        $func($this->im,null,$quality);
        // $name = time() . rand(1, 9999) . '.png';
        // $dir = realpath('./') . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR;
        // if (!is_dir($dir)) {
        //     mkdir($dir, 0777, true);
        // }
        // $func($this->im,$dir.$name);
        // return $name;
    }
    /**
     * 合并图像
     *
     * @return void
     */
    private function merge()
    {
        $this->im = imagecreatetruecolor($this->bg_width, $this->bg_height*3);
        imagecopy($this->im, $this->im_bg,0, 0 , 0, 0, $this->bg_width, $this->bg_height);
        imagecopy($this->im, $this->im_slide,0, $this->bg_height , 0, 0, $this->mark_width, $this->bg_height);
        imagecopy($this->im, $this->im_fullbg,0, $this->bg_height*2 , 0, 0, $this->bg_width, $this->bg_height);
        imagecolortransparent($this->im, 0);//16777215
    }
    /**
     * 背景图像
     *
     * @return void
     */
    private function bg()
    {
        $file_mark = dirname(__FILE__).'/img/mark.png';
        $im = imagecreatefrompng($file_mark);
        header('Content-Type: image/png');
        //imagealphablending( $im, true);
        imagecolortransparent($im,0);//16777215
        //imagepng($im);exit;
        imagecopy($this->im_bg, $im, $this->x, $this->y  , 0  , 0 , $this->mark_width, $this->mark_height);
        imagedestroy($im);
    }
    /**
     * 创建图像
     *
     * @return void
     */
    private function create()
    {
        $file_mark = dirname(__FILE__).'/img/mark2.png';
        $img_mark = imagecreatefrompng($file_mark);//内置php 函数编辑图片
        imagecopy($this->im_slide, $this->im_fullbg,0, $this->y , $this->x, $this->y, $this->mark_width, $this->mark_height);
        imagecopy($this->im_slide, $img_mark,0, $this->y , 0, 0, $this->mark_width, $this->mark_height);
        imagecolortransparent($this->im_slide,0 );//16777215
        //header('Content-Type: image/png');
        //imagepng($this->im_slide);exit;
        imagedestroy($img_mark);
    }
}
